import { getDownloadPath } from "@/apis/util";

/**
 * Checks if `value` is the
 * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
 * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
 *
 * @static
 * @memberOf _
 * @since 0.1.0
 * @category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
 * @example
 *
 * _.isObject({});
 * // => true
 *
 * _.isObject([1, 2, 3]);
 * // => true
 *
 * _.isObject(_.noop);
 * // => true
 *
 * _.isObject(null);
 * // => false
 */
export function isObject(value: any) {
  let type = typeof value;
  return value != null && (type == "object" || type == "function");
}

const excludeKeys = ["$$hashKey"];

export function traverseArray(
  obj: any,
  isObj?: boolean,
  attr?: string,
  res?: any,
) {
  //数据初始化
  res = res ? res : {};
  attr = attr ? attr : "";
  //执行数据转换
  if (
    obj &&
    isObject(obj) &&
    obj.toString().indexOf("[object File]") === -1 &&
    obj.toString().indexOf("[object Blob]") === -1
  ) {
    let attrRes;
    if (Array.isArray(obj)) {
      //是数组转换成[?]的形式
      for (let [index, item] of (obj as []).entries()) {
        attrRes = attr; //备份属性名称
        attr += "[" + index + "].";
        res = traverseArray(item, isObj, attr, res);
        attr = attrRes; //还原属性名称
      }
    } else {
      for (let index of Object.keys(obj)) {
        let item = obj[index];
        attrRes = attr;
        if (
          isObject(item) &&
          item.toString().indexOf("[object File]") === -1 &&
          item.toString().indexOf("[object Blob]") === -1 &&
          !Array.isArray(item) &&
          isObj
        ) {
          //如果item是一个对象且不是一个数组就增加点,否则增加[?]
          attr += index + ".";
        } else {
          attr += index;
        }
        res = traverseArray(item, isObj, attr, res);
        attr = attrRes;
      }
    }
  } else {
    if (!excludeKeys.includes(attr)) {
      res[attr] = obj;
    }
  }
  return res;
}

/**
 * 转换枚举类型为字符串
 * @param obj 要转换的对象
 */
export function overwriteEnumDeep(obj: any): any {
  if (Array.isArray(obj)) {
    for (let [index, item] of obj.entries()) {
      obj[index] = overwriteEnumDeep(item);
    }
  } else {
    for (let key of Object.keys(obj || {})) {
      let item = obj[key];
      if (item && item.name && item.disName && item.ordinal !== undefined) {
        obj[key] = item.name;
      } else if (isObject(item)) {
        if (Array.isArray(item)) {
          for (let [index, i] of item.entries()) {
            item[index] = overwriteEnumDeep(i);
          }
        } else {
          obj[key] = overwriteEnumDeep(item);
        }
      }
    }
  }

  return obj;
}

export function showCity(province: string, city: string) {
  if (city === "市辖区") {
    return province;
  } else {
    return city;
  }
}

//px单位转换为rpx
export function px2rpx(px: number) {
  //获取比例
  let scale = uni.upx2px(100) / 100;
  return px / scale;
}

/**
 * 获得一个特殊格式的对象
 * @param fileInfo {
 *       id: "787024948795801600",
 *       name: "HybhvDUrIyxl19cbc9135be6e1140ea7a4a36474f603.png",
 *       suffix: ".png",
 *       type: { ordinal: 4, name: "picture", disName: "图片" },
 *       createTime: "2024-12-11 18:36:10",
 *       updateTime: "2024-12-11 18:36:10",
 *       address:
 *         "ud-tms-web/picture/787024948598669312/HybhvDUrIyxl19cbc9135be6e1140ea7a4a36474f603.png",
 * }
 */
export function getUploadObj(fileInfo: any) {
  const response = {
    status: 200,
    message: "操作成功",
    data: fileInfo,
  };
  return {
    name: "",
    progress: 100,
    response: JSON.stringify(response),
    status: "上传成功",
    statusCode: 3,
    uid: uni.$tm.u.getUid(),
    url: getDownloadPath(fileInfo.address || ""),
  };
}
